<script>

var idDisablePedido = 0;
var lastClick = "";

function showOpt(id)
{
	$(".opt_" + id).slideDown("fast");

	hideOpt(lastClick);
}

function listaMesas()
{
	if($(".disable_mesa").css("display") == "block")
	{
		$(".disable_mesa").hide();
	}
	else
	{
		$('.msgMesas').hide(); 
		$('.listmesa').fadeIn('fast');
	}
}

function hideOpt(id)
{
	$(".opt_" + id).slideUp("fast");
}

function openPedido(mesa)
{
	$("#general").hide();

	$.post('pedido/pedido', {cod_mesa: mesa}, function(req)
	{
		$("#editPedido").html(req).fadeIn("fast");
	});
}

function openHistorico(id)
{
	$("#desac_btn").hide();

	$(".bg_transp").show();

	$.post('pedido/list', {mesa: id}, function(res)
	{
		$("#resumList").html(res);
		$(".resumDiv").fadeIn("fast");
	});
}

function disactivePedido()
{
	var forma_pagamento = $("input:radio[name=fp_radio]:checked").val();
	
	$(".menu_cnew").click();
	msg({vmsg: "Mesa <b>" + lastClick + "</b> Fechada.", time: 5000});
	
	$.post('pedido/disactive', {cod_usuario: user.caf01_cod, cod_pedido: idDisablePedido, forma_pagamento: forma_pagamento}, function(req)
	{
		$(".bg_transp").hide();
		$(".resumDiv").fadeOut("fast");
		
		printReceive2(idDisablePedido);


		//$("#editPedido").html(req).fadeIn("fast");
	});
}

function closePedido(id)
{
	idDisablePedido = id;
	
	$(".bg_transp").show();

	/*$.post('pedido/list', {cod: id}, function(res)
	{
		$("#resumList").html(res);
		$(".resumDiv").fadeIn("fast");
	});*/
}

function mostrarFormaPagamento()
{
	$.post('pedido/formas-pagamento', {numeroMesa: lastClick}, function(html){
		$("#ajax-formas-pagamento").html(html);
	});
}

function printReceive()
{
	window.open("imprimir/imprimirtxt/cod/" + idDisablePedido, "_blank");
}

function printReceive2(id)
{
	window.open("imprimir/imprimirtxt/cod/" + id, "_blank");
}

</script>

<div id="general" style="width: 780px; margin: 0 auto; overflow: auto;">
<div style="margin-bottom: 10px; height: 30px;">
<div style="float: right; text-align: right; width: 200px; margin-top: 10px; margin-right: 3px;">
	<label id='load_subproduct' style='display: none;'>&nbsp;<img src='<?php $this->base; ?>/public/images/ajax-loader.gif' /></label>
</div>
<div style="float: left; width: 400px; font-size: 22px; margin-top: 8px;">
	Fechar Mesa
</div>
</div>
<hr />
<div>
	<?php 
	
	$entro = false;
	$arr = array();
	$caf13 = new caf13();
	$arr_caf13=$caf13->find(1)->current()->toArray();
	
	$cod_mesa = "";
	
	foreach ($this->arr_pedido as $key => $val)
	{
		$arr[] = $val['caf02_mesa'];
		$user[$val['caf02_mesa']] = $val['caf01_user'];
		$user_cod[$val['caf02_mesa']] = $val['caf01_cod'];
		$cod[$val['caf02_mesa']] = $val['caf02_cod'];
		
		if($val["caf02_mesa"] == $cod_mesa)
		{
			$valor[$val['caf02_mesa']] += $val['caf05_valor'];
			$desconto[$val["caf02_mesa"]] = $val['caf02_desconto'];
		}
		else
		{
			$valor[$val['caf02_mesa']] = $val['caf05_valor'];
			$desconto[$val["caf02_mesa"]] = $val['caf02_desconto'];
		}
		
		$cod_mesa = $val["caf02_mesa"];
	}
	
	for($i = $arr_caf13["caf13_mesa_incial"]; $i < $arr_caf13["caf13_mesa_final"]; $i++)
	{
		$used = "";
		$bg = "";
		
		if(array_search($i, $arr) === false)
		{
		?>
		<div class="listmesa disable_mesa" style="float: left; display: none;">
<div
	onclick="lastClick = '<?php echo $i; ?>'; closePedido('<?php echo $cod[$i]; ?>'); $('.resumDiv').hide(); mostrarFormaPagamento();"
	class="button ui-corner-all" type="button"
	style="font-size: 12px; padding-top: 15px; text-align: center; width: 90px; height: 70px; margin: 5px; border: 1px solid gray; border-top: 0; border-right: 0;">
				Mesa <?php echo $i; ?>

			</div>
<div
	style="opacity: 0.9; position: absolute; z-index: 1; display: none; text-align: left; font-size: 24px; width: 166px; border: 1px solid #B0ADAD; background-color: white; margin-left: 3px;"
	class="ui-widget ui-corner-all unit opt_<?php echo $i; ?>">
<div onclick="openHistorico('<?php echo $i; ?>');"
	style="border-bottom: 1px solid #B0ADAD; padding: 5px;">Ultima Ordem</div>
</div>
</div>
		<?
		}
		else 
		{
			$entro = true;
		?>
		<div class="listmesa" style="float: left; <?php echo $bg; ?>">
<div
	onclick="lastClick = '<?php echo $i; ?>'; closePedido('<?php echo $cod[$i]; ?>'); mostrarFormaPagamento();"
	class="button ui-corner-all" type="button"
	style="font-size: 12px; padding-top: 10px; text-align: center; width: 90px; 
	height: 85px; margin: 5px; border: 1px solid gray; border-top: 0; border-right: 0;">
<font style="">
				<?php
				if($this->userId == $user_cod[$i] || $this->gerente)
				{
					echo "<b>{$user[$i]}</b>";
				} else{
					echo "$user[$i]";
				}
				?>
				</font> <br />
<strong>Mesa <?php echo $i; ?></strong> <br />
<font color="red">R$<?php echo Num::money($valor[$i] - $desconto[$i]); ?></font>
<div>
	cod.<?php echo $cod[$i]; ?>
</div>
</div>

</div>
	<?
		}
	}
	
	if(!$entro)
	{
		echo $this->partial("helpers/msgvazio.phtml", array("msg" => "Nenhuma mesa em uso."));
	}
	
	?>
	
	<div style="margin-top: 10px;"></div>
</div>
</div>